package cn.mdx.service;

import cn.mdx.common.beans.User;
import cn.mdx.common.exceptions.CheckException;
import cn.mdx.common.utils.CheckUtil;
import cn.mdx.common.utils.FileUtil;
import cn.mdx.mapper.SkilluserMapper;
import cn.mdx.mapper.SkillusersMapper;
import cn.mdx.pojo.Skilluser;
import cn.mdx.pojo.Skillusers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 谢模全
 */
@Service
public class SkillUserService {

    private static final Logger logger = LoggerFactory.getLogger(SkillUserService.class);

    @Autowired
    private SkilluserMapper skilluserMapper;

    @Autowired
    private SkillusersMapper skillusersMapper;

    /**
     * 技术方登录
     * @param username 用户名
     * @param password 密码
     * @return 用户基本信息表
     */
    public void selectByPassword( String username, String password,HttpSession session) {
        CheckUtil.notNull(username,"用户名不能为空");
        CheckUtil.notNull(password,"密码不能为空");
        logger.info("[登录中，参数为：{用户名：{}，密码：xxxxxx}]",username);
        Skilluser skilluser = skilluserMapper.selectByPassword(username,password);
        if (skilluser == null) {
            logger.info("[登录失败，账号或密码错误]");
            CheckUtil.fail("账号或密码错误");
        } else if(skilluser.getPower() == 0 || skilluser.getPower() == 3) {
            User user = new User();
            user.setPower(skilluser.getPower());
            user.setUsername(skilluser.getUsername());
            user.setId(skilluser.getUid());
            session.setAttribute("user",user);
            logger.info("[用户：<{}>登录成功]",user.getUsername());
        } else  {
            logger.info("未知错误，请重新登录");
            CheckUtil.fail("未知错误，请重新登录");
        }
    }

    /**
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @param uid 用户id
     */
    public void setPassword(String oldPassword, String newPassword,Integer uid){
        CheckUtil.notNull(oldPassword,"参数不能为空");
        CheckUtil.notNull(newPassword,"参数不能为空");
        CheckUtil.notNull(uid,"参数不能为空");
        logger.info("[修改密码中]");
        if(!oldPassword.trim().equals(newPassword.trim())){
            boolean is = skilluserMapper.selectByPrimaryKey(uid).getPassword().trim().equals(oldPassword.trim());
            if(is) {
                Skilluser skilluser = new Skilluser();
                skilluser.setUid(uid);
                skilluser.setPassword(newPassword.trim());
                skilluserMapper.updateByPrimaryKeySelective(skilluser);
                logger.info("[修改成功]");
            } else {
                logger.info("[修改失败]");
                CheckUtil.fail("原密码错误");
            }
        }
    }

    /**
     *
     * @param skilluser 用户基础资料
     * @param skillusers 用户详细资料
     * @param pic 图片文件
     * @throws IOException
     */
    public void setUser(Skilluser skilluser, Skillusers skillusers, MultipartFile pic, HttpSession session,Integer uid) throws IOException {
        //username和power两个属性必须为空
        CheckUtil.mustNull(skilluser,"请求数据异常","username&power");
        //audit属性必须为空
        CheckUtil.mustNull(skillusers,"","audit");

        logger.info("[修改用户基本信息中，参数[{},{}]]",skilluser,skillusers);

        skilluser.setUid(uid);
        skillusers.setUid(uid);
        String path = "img/3-"+uid+"/";
        String img = null;
        img = FileUtil.saveImgFile(session,pic,path);
        String delImg = null;
        if (img!=null) {
            delImg = skillusers.getImg();
            skillusers.setImg(img);
        }
        skilluserMapper.updateByPrimaryKeySelective(skilluser);
        skillusersMapper.updateByPrimaryKeySelective(skillusers);
        FileUtil.deleteFile(session,delImg);
        logger.info("[修改成功]");
    }
}

